import { ref } from "vue";
import instance from "@/api";
import { ElMessage } from "element-plus";
import { emitter } from "@/utils/emitter";
let showSelectTime = ref(false)
const showDate = ref('')
const selectTimeResult=ref('')
let userInfo: any = null
function selectTime(item:object){
    showSelectTime.value = true
    userInfo = item
}

async function ConfirmTime(){
    userInfo.arrivalTime = showDate.value + selectTimeResult.value
    showSelectTime.value = false
    const { id } = userInfo
    const obj = {
        time: userInfo.arrivalTime,
        staff: userInfo.serviceStaff ? [Number(userInfo.serviceStaff)] : [],
        payStatus: userInfo.payStatus || 0
    }
    try {
        const res = await instance.put(`/api/order/upDateOrder/${id}`, obj)
        const { status } = res
        if(status === 200){
            ElMessage.success('分配成功')
            if(userInfo.serviceStaff) emitter.emit('upDateTab')
        } 
        else ElMessage.error('分配失败')
    } catch (error) {
        console.error('分配时间失败:', error)
        ElMessage.error('分配失败')
    }
}
export default function(){
    return { showSelectTime, selectTime, showDate, selectTimeResult, ConfirmTime }
}